package optimizers.ganeat.jankenpon;

import java.util.concurrent.Callable;
import fitness.EloRecord;
import fitness.EloRecord.Result;
import fitness.EloResult;

public class JankenponELOMatch implements Callable<Object> {

	private JankenponIndividual indiv1;
	private JankenponIndividual indiv2;

	public JankenponELOMatch(JankenponIndividual indiv1, JankenponIndividual indiv2){
		this.indiv1 = indiv1;
		this.indiv2 = indiv2;
	}
	
	@Override
	public Object call() throws Exception {
		// TODO Auto-generated method stub
		
		JankenponGame result;

		if(indiv2 == null){
			return null;//weird
		}
			result = indiv1.competeAgainst(indiv2);
		
			JankenponMatchResult fallouts = new JankenponMatchResult();
		if(result.score > ((double)ConstantsJankenpon.rounds)/2.0 || indiv2 == null){
			fallouts.score = result.score;
			fallouts.winner = indiv1;
			fallouts.loser = indiv2;
			((EloResult) indiv1.fitnessResult).addResult(new EloRecord(indiv2.fitnessResult.finalResult(),result.iplayed,result.uplayed,Result.VICTORY));
			((EloResult) indiv2.fitnessResult).addResult(new EloRecord(indiv1.fitnessResult.finalResult(),result.uplayed,result.iplayed,Result.DEFEAT));
			
		} else if (result.score ==  ((double)ConstantsJankenpon.rounds)/2.0){
			
		fallouts.winner = indiv2;
		fallouts.loser = indiv1;
		fallouts.score = ConstantsJankenpon.rounds - result.score;
		((EloResult) indiv1.fitnessResult).addResult(new EloRecord(indiv2.fitnessResult.finalResult(),result.iplayed,result.uplayed,Result.DRAW));
		((EloResult) indiv2.fitnessResult).addResult(new EloRecord(indiv1.fitnessResult.finalResult(),result.uplayed,result.iplayed,Result.DRAW));
		} else {
			fallouts.winner = indiv2;
			fallouts.loser = indiv1;
			fallouts.score = ConstantsJankenpon.rounds - result.score;
			((EloResult) indiv1.fitnessResult).addResult(new EloRecord(indiv2.fitnessResult.finalResult(),result.iplayed,result.uplayed,Result.DEFEAT));
			((EloResult) indiv2.fitnessResult).addResult(new EloRecord(indiv1.fitnessResult.finalResult(),result.uplayed,result.iplayed,Result.VICTORY));
		}
		return fallouts;
	}

}

